package org.dimi.controller;

import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import org.dimi.model.Customer;
import org.dimi.model.User;

/**
 * Session Bean implementation class UserEJB
 */

@Stateful
@Model
public class UserEJB implements UserEJBRemote, UserEJBLocal {

	@Inject
	private Logger log;

	@Inject
	private EntityManager em;

	private User newUser;

	/**
	 * Default constructor.
	 */
	public UserEJB() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the newUser
	 */
	@Produces
	@Named
	public User getNewUser() {
		return newUser;
	}

	/**
	 * @param user
	 *            the user to save him
	 */
	public void saveUser() {
		log.info("Registering " + newUser.getLastName());
		em.persist(newUser);
	}

	@PostConstruct
	public void initNewUser() {
		newUser = new Customer();
	}
}
